<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("userFunction.php");

$errMsg = "";

$id_user =  sxREQUEST("id_user");
if(isset($_REQUEST["id"])){
	$id_user = $_REQUEST["id"];
}
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= userProfIns();	
}
$sSQL = "SELECT u.*  " .
		" FROM user u " .
		" WHERE u.id_user=$id_user ";
$objSDAO->query($sSQL,"sel");
$rowU = $objSDAO->fetch("sel");

$sSQL = "SELECT p.description, p.id_profile , up.id_user " .
		" FROM profile p " .
		" LEFT JOIN user_profile up ON p.id_profile=up.id_profile AND up.id_user=$id_user " .
		" ORDER BY p.description";
$objSDAO->query($sSQL,"sel");

if (!$rowU) {
	$errMsg .= _t("Impossibile caricare i dati dell'utente");
}

//output HTML
$aOpz = array("tit2"=>_t("Lista dei profili dell'utente"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea =  document.getElementById("messArea");
		
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
<?php
echo(bodyHTML($aOpz));
//output comandi
$aCmd = array("save"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati"),
										"text"=>_t("salva"),
										"perm"=>"upduser"),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0),
										"perm"=>"")
					);
echo(commandsHTML($aCmd));	
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"userProfileIns.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"id_user\" id=\"id_user\" value=\"$id_user\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
//output dei dati
if($errMsg == ""){
	$numElem = 0;
	echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>"._t("Profili assegnati all'utente")." (".$rowU['uid'].") ".
			$rowU['name']." ".$rowU['last_name']."</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"tabh01\" width=\"1%\">&nbsp;</td>" .
			"<td class=\"tabh01\" width=\"99%\">"._t("Profili")."</td>" .
		"</tr>\n");
	while($row = $objSDAO->fetch("sel")) {
		if($objSDAO->error == ""){
			$numElem ++;
			$chk = "";
			if(trim($row["id_user"]) != "" && $row["id_user"] == $id_user)
				$chk = "CHECKED";
			echo("<tr>" .
					"<td class=\"text01\">" .
					"<input class=\"text01\" type=\"checkbox\" name=\"id_profile_".$row["id_profile"]."\" id=\"id_profile_".$row["id_profile"]."\" $chk></td>" .
					"<td class=\"text01\">".sxHE($row["description"])."</td>" .
				"</tr>\n");
		}else{
			echo("<tr>" .
					"<td class=\"text01\" colspan=\"2\">".$objSDAO->error."</td>" .
				"</tr>\n");
		}
	}
	if($numElem == 0)
		echo("<tr>" .
				"<td class=\"text01\" colspan=\"2\">"._t("Nessun profilo definito")."</td>".
			"</tr>\n");
	echo("</table>\n");
}else{
	
}
echo(commandsHTML($aCmd));
echo(footerHTML());
$objSDAO->close();
?>
